package com.wxsong.authplatform.authweb.controller;

import com.google.common.collect.Maps;
import com.wxsong.authplatform.authweb.model.test.User;
import com.wxsong.authplatform.authweb.service.test.impl.TestService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.Timestamp;
import java.util.Map;

@Controller
@RequestMapping("/test")
public class TestController {

    @Resource
    private TestService testService;


    @RequestMapping("/test1")
    @ResponseBody
    public Map test1(){
        Map<String, Object> resultMap = Maps.newHashMap();

        resultMap.put("info", "成功");
        resultMap.put("flag", true);

        return resultMap;
    }

    @RequestMapping("/index")
    public String index(Model model){
        model.addAttribute("info", "你好吗？");
        User user = testService.getById(1);
        model.addAttribute("data", user);
        return "index";
    }
    @RequestMapping("/index2")
    public String index2(Model model){
        return "test/index";
    }

    @RequestMapping("/query/{id}")
    @ResponseBody
    public Map query(@PathVariable Integer id, Model model){
        Map<String, Object> resultMap = Maps.newHashMap();
        User user = testService.getById(id);
        resultMap.put("info", "成功");
        resultMap.put("data", user);
        resultMap.put("flag", true);
        return resultMap;
    }
    @RequestMapping("/save")
    @ResponseBody
    public Map save(String name, Integer age, Model model){
        Map<String, Object> resultMap = Maps.newHashMap();
        User user = new User(name, age, new Timestamp(System.currentTimeMillis()));
        testService.save(user);
        resultMap.put("info", "成功");
        resultMap.put("data", user);
        resultMap.put("flag", true);
        return resultMap;
    }
    @RequestMapping("/update/{id}")
    @ResponseBody
    public Map update(@PathVariable Integer id, String name, Integer age, Model model){
        Map<String, Object> resultMap = Maps.newHashMap();
        User user = testService.getById(id);
        user.setName(name);
        user.setAge(age);
        testService.update(user);
        resultMap.put("info", "成功");
        resultMap.put("data", user);
        resultMap.put("flag", true);
        return resultMap;
    }
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public Map delete(@PathVariable Integer id, Model model){
        Map<String, Object> resultMap = Maps.newHashMap();
        int succesCount = testService.delById(id);
        resultMap.put("info", "成功");
        resultMap.put("data", succesCount);
        resultMap.put("flag", true);
        return resultMap;
    }


    public static void main(String[] args) {
        String parent = "%E7%99%BE%E5%BA%A6%E9%9B%86%E5%9B%A2";
        try {
            parent = URLDecoder.decode(parent, "UTF-8");
            System.out.println(parent);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
